package org.honeyweb.gsoc.pages;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.Service;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.honeyweb.gsoc.model.Organizations;
import org.honeyweb.gsoc.model.Users;
import org.honeyweb.gsoc.services.OrganizationsManager;
import org.honeyweb.gsoc.services.UsersManager;

public class CreateOrganization {
	
	private final Log logger = LogFactory.getLog(CreateOrganization.class);
	
	@Inject
	@Service("organizationsManager")
	private OrganizationsManager organizationsManager;
	
	@Inject
	@Service("usersManager")
	private UsersManager usersManager;
	
	@SessionState(create=true)
	private Users user;

	@Component
	private Form form;
	
	@Property
	private String name;
	
	String onSuccess(){
		int userId = user.getIdUser();
		organizationsManager.addOrganization(name,userId);
		Organizations organization = organizationsManager.getOrganizationByAdminId(userId);
		usersManager.updateUserOrganizationsId(organization.getId(),user.getIdUser());
		return "Profile";
	};
}
